package com.kmzx.one.project.config.listener;

import com.kmzx.one.common.utils.UUIDGenerator;
import com.kmzx.one.project.entity.UserShareMini;
import com.kmzx.one.project.entity.UserShareMiniDetail;
import com.kmzx.one.project.events.ShareMiniEvent;
import com.kmzx.one.project.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component(value = "shareMiniEventListener")
public class ShareMiniEventListener {


    @Autowired
    CustomerMapper customerMapper;

    @Async
    @EventListener(classes = {ShareMiniEvent.class})
    @Transactional(rollbackFor = Exception.class)
    public void defaultShareMiniEvent(ShareMiniEvent shareMiniEvent) {

        int tag = shareMiniEvent.getTag();

        UserShareMini shareMini = shareMiniEvent.getUserShareMini();
        String id = shareMini.getId();
        if(tag==1){
            // 新增
            UserShareMiniDetail miniDetail = new UserShareMiniDetail();
            miniDetail.setId(UUIDGenerator.generate()).setCreateTime(new Date())
                    .setMiniId(id).setUserId(shareMini.getUserId());
            customerMapper.insertUserShareMiniDetail(miniDetail);
            customerMapper.insertUserShareMini(shareMini);
        }
        if(tag==2){
            // 明细记录
            UserShareMiniDetail miniDetail = new UserShareMiniDetail();
            miniDetail.setId(UUIDGenerator.generate()).setCreateTime(new Date())
                    .setMiniId(id).setUserId(shareMini.getUserId());
            customerMapper.insertUserShareMiniDetail(miniDetail);
        }
    }
}
